#!/usr/bin/python
#coding:utf-8
#Name       : Credit_login.py
#Function   : Credit system
#Date       : 2015-8-13
#Author     : songbing

import sys
import pickle
import os
import calendar as cal
import time

xinyong=15000
login_user={'song':123456,
            'bing':654321,
            }
buy_time=[]
#登录函数
def login():
    while True:
        num=0
        user_name=raw_input("请输入你的信用卡账号：").strip()
        if not os.path.exists('Login_black.txt'):
            lock=open('Login_black.txt',"w+")
            lock.close()
        with open('Login_black.txt') as black_file:
            for i in black_file.readlines():
                if user_name in i:
                    print "您已被列入黑名单，请联系客服！"
                    sys.exit()
        while login_user.has_key(user_name) :
            user_pass=raw_input("请输入你的信用卡密码：")
            if user_pass.isdigit():
                user_pass=int(user_pass)
            else:
                print "请输入六位数字密码"
            if user_pass==login_user[user_name]:
                print "您的信用卡额度为%s " % xinyong
                if os.path.exists(user_name+'.pkl'):
                    pass
                else:
                    with open(user_name+'.pkl','w') as quota_file:
                        pickle.dump(xinyong,quota_file)

                break
            else:
                num+=1
                print "密码错误，请重试输入！"
            if num>=3:
                print "密码错误3次,已经锁定,请联系客服!"
                with open('Login_black.txt','w') as black_file:
                    black_file.write(user_name+'\n')
                    sys.exit()

        else:
            print "没有此账号!"
            continue
        return user_name

#功能函数
def Action():
        global liushui
        print '''
            功能选项
            Q: 提供取现功能，收取5%手续费
            C：查询可提前信用额度，余额和交易明细
            H: 提供现金还款功能
            S: 信用卡商城
            Z: 提供转账功能
            E: 退出
            '''.center(120)
        while True:
            choice=raw_input("请您输入您要操作的选项：").lower().strip()
            if choice=='q':
                Take_money(quota)
            elif choice=='c':
                with open(user_name+'.pkl') as quota_file:
                    dang_quota=int(pickle.load(quota_file))
                print "您当前可用额度%s" %(dang_quota)
                if Time():
                    if os.path.exists(user_name+'liushui.pkl'):
                        with open(user_name+'liushui.pkl') as liushuif:
                            buy_liu=pickle.load(liushuif)
                        print "购买商品记录：%s " % buy_liu
                    else:
                        pass
                else:
                    print "还未到出账单时间，账单时间是每月月底"
            elif choice=='h':
                Hua_mem()
            elif choice=='z':
                Zhuan_mem()
            elif choice=='s':
                Shop(quota)
            elif choice=='e':
                break
            else:
                print "暂时没有开通次选项"

#提现函数，
def Take_money(yuan_quota):
    while True:
        meney=raw_input("请您输入要提现的金额：")
        if meney.isdigit():
            meney=int(meney)
        else:
            print "请输入数字金额！"
            continue
        if meney>yuan_quota/2:
            print "Sorry,您输入的金额大于您的信用额度，请查询您的可提取的信用额度。"
            continue
        elif meney<=yuan_quota/2:
            print '您提现的金额为\033[31m%s\033[0m,您的提现手续费为\033[31m%s\033[0m.' %(meney,meney*0.05)
            sheng_quota=yuan_quota-meney-meney*0.05
            print '您的剩余额度\033[31m%s\033[0m' % sheng_quota
            with open(user_name+'.pkl','w') as quota_file:
                pickle.dump(sheng_quota,quota_file)
            with open(user_name+'.pkl') as quota_file:
                quota=int(pickle.load(quota_file))
        break

#定义时间，每月的最后一天出账单
def Time():
    data=int(time.strftime('%d'))
    year = int(time.strftime('%Y'))
    mou = int(time.strftime('%m'))
    d = cal.monthrange(year, mou)
    if d[1]==data:
        #return "今天提示 %d 最后一天" % mou
        return True
    else:
        #return "今天是 %d 月的 %d 号" %(mou,data)
        return False

#定义还款函数
def Hua_mem():
    while True:
        huan_quota=raw_input('请输入您的还款金额：')
        if huan_quota.isdigit():
            huan_quota=int(huan_quota)
        else:
            print "请您输入数字金额"
            continue
        with open(user_name+'.pkl') as quota_file:
            dang_quota=int(pickle.load(quota_file))
        print "您当前可用额度%s" %(dang_quota)
        with open(user_name+'.pkl','w') as quota_file:
            pickle.dump(dang_quota+huan_quota,quota_file)
        with open(user_name+'.pkl') as quota_file:
            dang_quota=int(pickle.load(quota_file))
        print "您还款以后，当前可用额度%s" %(dang_quota)
        break


#转账功能函数

def Zhuan_mem():
    while True:
        zhuan_user=raw_input("请您提供需要转账用户：")
        while login_user.has_key(zhuan_user):
            zhuan_memory=raw_input("请您输入要转的金额：")
            if zhuan_memory.isdigit():
                zhuan_memory=int(zhuan_memory)
            else:
                print "请您输入数字金额"
                continue
            if os.path.exists(zhuan_user+'.pkl'):
                with open(zhuan_user+'.pkl') as quota_file:
                    zhuan_mem_qian=int(pickle.load(quota_file))
                with open(zhuan_user+'.pkl','w') as quota_file:
                    pickle.dump(zhuan_mem_qian+zhuan_memory,quota_file)
                with open(user_name+'.pkl') as quota_file:
                    zhuan_mem=int(pickle.load(quota_file))
                with open(user_name+'.pkl','w') as quota_file:
                    pickle.dump(zhuan_mem-zhuan_memory,quota_file)
                with open(user_name+'.pkl') as quota_file:
                    zhuan_mem=int(pickle.load(quota_file))
                    print "你当前的余额：" ,zhuan_mem
                break
            else:
                with open(zhuan_user+'.pkl','w') as quota_file:
                        pickle.dump(xinyong,quota_file)
                with open(zhuan_user+'.pkl') as quota_file:
                    zhuan_mem_qian=int(pickle.load(quota_file))
                with open(zhuan_user+'.pkl','w') as quota_file:
                    pickle.dump(zhuan_mem_qian+zhuan_memory,quota_file)
                with open(user_name+'.pkl') as quota_file:
                    zhuan_mem=int(pickle.load(quota_file))
                with open(user_name+'.pkl','w') as quota_file:
                    pickle.dump(zhuan_mem-zhuan_memory,quota_file)
                with open(user_name+'.pkl') as quota_file:
                    zhuan_mem=int(pickle.load(quota_file))
                    print "你当前的余额：" ,zhuan_mem
                break

        else:
            print "没有此用户，请您确定输入是否正确！"
            continue
        break

def Shop(price):
    while True:
        print "\033[32m欢迎来到信用卡商城\033[0m".center(110)
        print ('=' * width).center(100)
        print (header_format % (item_width,'\033[1;31mShop\033[0m',item_width,'\033[1;31mPrice\033[0m')).center(120)
        print ('-' * width).center(100)
        for shop in shop_list:
            #print ("\033[1;31m%s  %s\033[0m" % (shop, str(shop_price[shop_list.index(shop)])))
            print (header_format % (item_width,shop,price_with,shop_price[shop_list.index(shop)])).center(100)
        print ('=' * width).center(100)
        while True:
            buy_shop = raw_input("请输入你需要购买的商品名称:")
            if buy_shop in shop_list:
                newshop_price = shop_price[shop_list.index(buy_shop)]
                if price >= newshop_price:
                    price -= newshop_price
                    shop_buy.append(buy_shop)
                    buy_price.append(newshop_price)
                    print "你现经购买的商品:\033[95m %s \033[0m, 商品价格 \033[31m %s \033[0m" % (' '.join(shop_buy),buy_price)
                    buy_time.append(shop_buy_time)
                    liushui=''.join(str(zip(shop_buy,buy_price,buy_time)))
                    print "你还剩余的金额!\033[95m %s \033[0m" % (price)
                    if os.path.exists(user_name+'liushui.pkl'):
                        pass
                    else:
                        with open(user_name+'liushui.pkl','w') as liushuif:
                            pickle.dump(' ',liushuif)
                    while price < min(shop_price):
                        q = raw_input("资金不足,请输入 'quit' 退出!")
                        if q == "quit":
                            print "你已经购买的商品:\033[95m %s \033[0m" % (' '.join(shop_buy))
                            print "你剩余的金额!\033[95m %s \033[0m" % price
                            with open(user_name+'.pkl','w') as q:
                                pickle.dump(price,q)
                            with open(user_name+'liushui.pkl') as liushuif:
                                now=pickle.load(liushuif)
                            with open(user_name+'liushui.pkl','w') as liushuif:
                                pickle.dump(liushui+now,liushuif)
                            sys.exit()
                        else:
                            continue
                else:
                    print "\033[1;34;5m购买此商品，您的金额不足!\033[0m".center(100)
            elif buy_shop=='quit':
                with open(user_name+'.pkl','w') as q:
                    pickle.dump(price,q)
                with open(user_name+'liushui.pkl','r') as liushuif:
                    now1=pickle.load(liushuif)
                with open(user_name+'liushui.pkl','w') as liushuif:
                    pickle.dump(liushui+now1,liushuif)
                break
            else:
                print "\033[32m没有此商品，请您重新购买!\033[0m"
        break

shop_list = ['car', 'book', 'iphone', 'Computer', 'tea']
shop_price = [598109, 108, 6000, 12000, 55]
shop_buy = []
buy_price = []
width=35
price_with=10
item_width=width-price_with
header_format='%-*s%*s'
format='%-*s%.2f'
user_name=login()
with open(user_name+'.pkl') as quota_file:
   quota=int(pickle.load(quota_file))
shop_buy_time=time.strftime('%Y-%m-%d:%H:%M:%S')
Action()


